//
//  LiveRoomViewController.swift
//  yunyou
//
//  Created by 袁利伟 on 16/11/6.
//  Copyright © 2016年 mac. All rights reserved.
//

import UIKit
import AlivcLiveVideo
import SnapKit
public let SCREEN_WIDTH = UIScreen.mainScreen().bounds.size.width
public let SCREEN_HEIGHT = UIScreen.mainScreen().bounds.size.height
class LiveRoomViewController: UIViewController {

    var livesession :AlivcLiveSession?
    private var timer:NSTimer?
    private var lastdist:CGFloat?
    
    private lazy var closeButton:UIButton = {
        let btn = UIButton()
        btn.setImage(UIImage(named: "live_close"), forState: UIControlState.Normal)
        btn.addTarget(self, action: #selector(LiveRoomViewController.closeButtonClick(_:)), forControlEvents: UIControlEvents.TouchUpInside)
        return btn
    }()
    

    override func viewDidLoad() {
        super.viewDidLoad()
        setUI()
        testLiveConfig()
    }

    func setUI() {
        self.view.addSubview(closeButton)
        closeButton.snp_makeConstraints { (make) in
            make.top.equalTo(self.view.snp_top).offset(30)
            make.right.equalTo(self.view.snp_right).offset(-12)
            make.width.equalTo(17)
            make.height.equalTo(17)
            
        }
        
    }

    
    //MARK: - 配置阿里直播
    func testLiveConfig() {
        
        let configuration = AlivcLConfiguration()
        configuration.url = "tuiliudizhi"
        configuration.videoMaxBitRate = 1500 * 1000
        configuration.videoBitRate = 600 * 1000
        configuration.videoMinBitRate = 400 * 1000
        configuration.audioBitRate = 64 * 1000
        configuration.videoSize = CGSize(width: SCREEN_WIDTH, height: SCREEN_HEIGHT)
        configuration.fps = 20
        configuration.preset = AVCaptureSessionPresetiFrame1280x720
        configuration.screenOrientation = .AlivcLiveScreenVertical
        configuration.position = .Front
        livesession = AlivcLiveSession(configuration: configuration)
        livesession!.delegate = self
        livesession?.alivcLiveVideoStartPreview()
        livesession?.alivcLiveVideoConnectServer()
        livesession?.enableSkin = true
        self.view.insertSubview((livesession?.previewView())!, atIndex: 0)
        
        
    }
    
    func closeLiveSession() {
        livesession?.alivcLiveVideoDisconnectServer()
        livesession?.alivcLiveVideoStopPreview()
        livesession?.previewView().removeFromSuperview()
        livesession = nil
        timer?.invalidate()
        timer = nil
        
    }
    
    @objc private func closeButtonClick(btn:UIButton) {
        
        self.dismissViewControllerAnimated(true, completion: nil)
        
        
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    

    

}

//MARK: - 阿里直播的代理
extension LiveRoomViewController:AlivcLiveSessionDelegate{
    

    func alivcLiveVideoLiveSession(session: AlivcLiveSession!, encodeAudioError error: NSError!) {
        
    }
    
    func alivcLiveVideoLiveSession(session: AlivcLiveSession!, openVideoError error: NSError!) {
        
    }
    
    func alivcLiveVideoLiveSession(session: AlivcLiveSession!, openAudioError error: NSError!) {
        
    }
    
    func alivcLiveVideoLiveSession(session: AlivcLiveSession!, encodeVideoError error: NSError!) {
        
    }
    
    func alivcLiveVideoLiveSessionNetworkSlow(session: AlivcLiveSession!) {
        
    }
    
    func alivcLiveVideoLiveSession(session: AlivcLiveSession!, error: NSError!) {
        
    }
    
    func alivcLiveVideoLiveSessionConnectSuccess(session: AlivcLiveSession!) {
    
    }

}
